<?php
define('FMIS_FILE_TYPE_UNKNOWN', 0);
define('FMIS_FILE_TYPE_DOCUMENT', 1);
define('FMIS_FILE_TYPE_IMAGE', 2);
define('FMIS_FILE_TYPE_AUDIO', 3);
define('FMIS_FILE_TYPE_ARCHIVE', 4);


class File extends CI_Model {
	private $id;
	private $name;
	private $owner;
	private $path;
	private $type;
	private $permissions;

	public function __construct($name = '', $owner = '', $path = '', $type = '', $permissions = array()) {
		parent::__construct();
		$this->name = $name;
		$this->owner = $owner;
		$this->path = $path;
		$this->type = $type;
		$this->permissions = $permissions;
	}

	public function get_id() {
		return $this->id;
	}

	public function get_name() {
		return $this->name;
	}

	public function set_name($new) {
		$this->name = $new;
	}

	public function get_owner() {
		return $this->owner;
	}

	public function get_path() {
		return $this->path;
	}

	public function get_type() {
		return $this->type;
	}

	public function get_member_permission($member_id) {
		return $this->permissions[$member_id];
	}

	public function set_member_permissions($member_id, $perm, $enabled) {
		if(!isset($this->permissions[$member_id]))
			return;
		switch($perm) {
		case 'view':
		case 'delete':
			$this->permissions[$member_id][$perm] = $enabled;
		}
	}

	public function insert() {
		$file_query = $this->db->query("INSERT INTO exp_fmis_file VALUES (?, ?, ?, ?, ?)",
			array(
			    NULL,
			    $this->name,
			    $this->owner,
			    $this->path,
			    $this->type
			)
		);

		$file_result = $file_query->result_array();

		$usergroup_perms = array();

		foreach($this->permissions as $id => $perms)
			$this->db->query("INSERT INTO exp_fmis_file_permission VALUES(?, ?, ?, ?)",
				array(
					$file_result['ID'],
					$id,
					$perms['view'],
					$perms['delete']
				)
			);
	}
}
